package com.itgungnir.designpattern.factory.abstractfactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

import com.itgungnir.designpattern.factory.ac.AC;
import com.itgungnir.designpattern.factory.car.BMW;

public class Test {
	private static BMW bmw;
	private static AC ac;

	private static final String[] classes = {
			"com.itgungnir.designpattern.factory.abstractfactory.Factory523",
			"com.itgungnir.designpattern.factory.abstractfactory.Factory320" };

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请选择要生产的产品的型号：");
		System.out.println("1、523型号");
		System.out.println("2、320型号");
		int type = scanner.nextInt();

		try {
			Class<?> factory = Class.forName(classes[type - 1]);
			Method createBMW = factory.getMethod("createBMW");
			Method createAC = factory.getMethod("createAC");
			bmw = (BMW) createBMW.invoke(factory.newInstance());
			ac = (AC) createAC.invoke(factory.newInstance());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		}

		if (bmw != null && ac!=null) {
			bmw.introduce();
			ac.introduce();
		} else {
			System.out.println("没有这个型号的产品！");
		}

		scanner.close();
	}
}